package com.jtlrm.ckd.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.base.sdk.base.net.CommonObserver;
import com.base.sdk.widget.TitleBar;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.acitvity.BaseActivity;
import com.jtlrm.ckd.entity.event.FinishPawwordMessage;
import com.jtlrm.ckd.mvp.model.UserModel;
import com.jtlrm.ckd.mvp.model.dao.UserHelper;
import com.jtlrm.ckd.util.EventBusUtil;

import butterknife.BindView;

public class ChangePasswordActivity extends BaseActivity {
    @BindView(R.id.change_password_title)
    TitleBar titleBar;
    @BindView(R.id.password)
    EditText passwordE;
    @BindView(R.id.new_password)
    EditText newPasswordE;
    @BindView(R.id.new_password2)
    EditText newPasswordE2;
    UserModel userModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_change_password);
    }

    @Override
    protected void initView() {
        titleBar.tvMiddle.setText("修改密码");
        titleBar.imgLeft.setVisibility(View.VISIBLE);
        titleBar.imgLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        userModel = new UserModel();
    }

    @Override
    protected void obtainData() {

    }

    @Override
    protected void initEvent() {

    }

    public void submit(View view) {
        String password = passwordE.getText() + "";
        String newPassword = newPasswordE.getText() + "";
        String newPassword2 = newPasswordE2.getText() + "";
        if (!notEmpty(password) || !notEmpty(newPassword) || !notEmpty(newPassword2)) {
            showToast("请输入密码");
            return;
        }
        if (!newPassword.equals(newPassword2)) {
            showToast("设置新密码必须一致");
            return;
        }

        if (password.length() < 6 || newPassword.length() < 6) {
            showToast("密码至少六位");
            return;
        }
        if (password.length() > 16 || newPassword.length() > 16) {
            showToast("密码最长不超过16位");
            return;
        }
        showLoadingDialog("修改中");
        userModel.changePassword(password, newPassword, new CommonObserver<Object>() {
            @Override
            public void onError(int errType, String errMessage) {
                showToast(errMessage);
                dismissLoadingDialog();
            }

            @Override
            public void onResult(Object data) {
                dismissLoadingDialog();
                EventBusUtil.postEvent(new FinishPawwordMessage());
                UserHelper.getInstance(context).loginOut();
                showToast("修改成功");
                startActivity(new Intent(context, LoginActivity.class));
                finish();
            }
        }, lifecycleSubject);
    }


}
